package com.fan.analysis.config;


import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis配置
 *
 * @author fan
 * @date 2021/09/25 20:34
 **/
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    /**
     * 配置redis缓存
     *
     * @param factory redis连接工厂
     * @return redis缓存管理类
     */
    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory factory) {

        return RedisCacheManager.create(factory);
    }

    /**
     * 配置redis连接方式
     *
     * @param factory redis连接工厂
     * @return redis模板类
     */
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        redisTemplate.setConnectionFactory(factory);
        //key序列化
        redisTemplate.setKeySerializer(redisSerializer);
        //value序列化
        redisTemplate.setValueSerializer(redisSerializer);
        //value hashmap序列化
        redisTemplate.setHashKeySerializer(redisSerializer);
        //key hashmap序列化
        redisTemplate.setHashValueSerializer(redisSerializer);
        return redisTemplate;
    }


}
